package com.dev_tang.controller;

import com.dev_tang.vo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lemontea on 18-6-3.
 */
@Controller
public class UserController {

    @RequestMapping(value = "/subLogin", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String subLogin(User user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            // 存储cookie 自动登录
            token.setRememberMe(user.isRememberMe());
            subject.login(token);
        } catch (AuthenticationException e) {
            return e.getMessage();
        }

        if (subject.hasRole("admin")) {
            return "有admin权限";
        }
        return "无admin权限";
    }

    /*@RequiresRoles("admin")
    @RequestMapping(value = "/testRole", method = RequestMethod.GET)
    @ResponseBody
    public String testRole() {
        return "testRole success";
    }

    @RequiresRoles("admin1")
    @RequestMapping(value = "/testRole1", method = RequestMethod.GET)
    @ResponseBody
    public String testRole1() {
        return "testRole1 success";
    }

    @RequiresPermissions("user:delete")
    @RequestMapping(value = "/testUserDelete", method = RequestMethod.GET)
    @ResponseBody
    public String testUserDelete() {
        return "testUserDelete success";
    }

    @RequiresPermissions("user:select")
    @RequestMapping(value = "/testUserSelect", method = RequestMethod.GET)
    @ResponseBody
    public String testUserSelect() {
        return "testUserSelect success";
    }*/

    @RequestMapping(value = "/testRole", method = RequestMethod.GET)
    @ResponseBody
    public String testRole() {
        return "testRole success";
    }

    @RequestMapping(value = "/testRole1", method = RequestMethod.GET)
    @ResponseBody
    public String testRole1() {
        return "testRole1 success";
    }

    @RequestMapping(value = "/testUserDelete", method = RequestMethod.GET)
    @ResponseBody
    public String testUserDelete() {
        return "testUserDelete success";
    }

    @RequestMapping(value = "/testUserSelect", method = RequestMethod.GET)
    @ResponseBody
    public String testUserSelect() {
        return "testUserSelect success";
    }
}
